var base = require('/base_info.js');
var utils = require('/utils/index.js');
var extJson = my.getExtConfigSync();
Page({
  data: {
    activeIndex: 0,
    reason: ['服务态度差', '服务效率低', '性价比不高', '装修老旧', '房间有异味', '隔音效果差', '房间没打扫干净', 'Wi-Fi信号差', '空调不给力', '洗漱用品档次低', '其他'],
    reasonActive: [],
    roomNumber: '',
    remark: '',
    reaseList: [],
    imgList: [],
    imgurl: base.imgUrl1,
    imgListArr: []
  },
  // 点击导航切换
  navClick(e) {
    this.setData({
      activeIndex: e.currentTarget.dataset.id
    });
    if (this.data.activeIndex == 1) {
      this.getTeaseList();
    }
  },
  choseImg() {
    let that = this;
    my.chooseImage({
      count: 3,
      success: (res) => { // 成功选择图片的回调
        console.log(JSON.stringify(res));
        // 调用上传图片的api
        that.upImg(res.apFilePaths);
      }
    });
  },
  // 上传图片
  upImg(path) {
    let that = this;
    var arr = [];
    console.log(path);
    console.log(base.baseurl + "?i=" + base.i + "&c=" + base.c + "&a=" + base.a + "&do=Upload&m=" + base.m);
    path.forEach((item, index) => {
      console.log(path[index]);
      my.uploadFile({
        // url: 'https://tzlxmin.tiaze.com/app/index.php?i=2&c=entry&a=phoneapp&do=Upload&m=zh_jdgjb&appid=2018111062092662&shopid=115&template_id=20181214000000001349266000300227',
        url: base.baseurl + '?i=' + base.i + '&c=' + base.c + '&a=' + base.a + '&do=Upload&m=' + base.m + '&appid=' + extJson.appId + '&shopid=' + extJson.shopId + '&template_id=' + extJson.miniShopId,
        fileType: 'image',
        fileName: "upfile",
        filePath: path[index],
        success: (res) => {  // 成功上传的回调
          console.log(res);
          var data = JSON.parse(res.data);
          if (data.message == 'success') {
            arr.push(data.data.path);
            if (index == (path.length - 1)) {
              that.setData({
                imgList: path,
                imgListArr: arr
              });
            };
          }
        },
        fail: (err) => {
          console.log(err);
        }
      });
    });
  },
  // 选择快速吐槽
  reasonSelect (e) {
    var reason = e.currentTarget.dataset.reason;
    var reasonActive = this.data.reasonActive;
    if (reasonActive.indexOf(reason) == -1) {
      reasonActive.push(reason);
    } else {
      var index = reasonActive.indexOf(reason);
      reasonActive.splice(index, 1);
    }
    this.setData({
      reasonActive: reasonActive
    });
  },
  // 房间号输入监听
  roomNumberInput (e) {
    this.setData({
      roomNumber: e.detail.value
    })
  },
  // 备注输入监听
  remarkInput(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  // 提交吐槽
  submit () {
    let that = this;
    if (that.data.room_number == "") {
      my.alert({
        content: '请输入房间号'
      });
      return false;
    } else if (that.data.reasonActive == "") {
      my.alert({
        content: '请选择糟点'
      });
      return false;
    };
    my.showLoading();
    utils.getData('Suggestion', {
      user_id: my.getStorageSync({
        key: 'uid', // 缓存数据的key
      }).data,
      name: my.getStorageSync({
        key: 'nickName', // 缓存数据的key
      }).data,
      content: that.data.remark,
      room_number: that.data.roomNumber,
      tags: that.data.reasonActive.join(),
      image_path: that.data.imgListArr.join()
    }, (res) => {
      my.hideLoading();
      if (res.data.message == 'success') {
        that.setData({
          roomNumber: '',
          remark: '',
          reasonActive: [],
          image_path: [],
          imgList: [],
          imgListArr: []
        });
        my.navigateTo({
          url: './tease-success'
        });
        return false;
      }
      my.alert({
        content: res.message
      });
    }, (err) => {
      my.hideLoading();
      console.log(err);
    });
  },
  // 吐槽列表
  getTeaseList () {
    let that = this;
    my.showLoading();
    utils.getData('ShowSuggestion', {
      user_id: my.getStorageSync({
        key: 'uid', // 缓存数据的key
      }).data
    }, (res) => {
      res.data.data.forEach((item, index) => {
        my.hideLoading();
        res.data.data[index].img = item.image_path.split(',');
      });
      that.setData({
        reaseList: res.data.data
      });
    }, (err) => {
      my.hideLoading();
      console.log(err);
    });
  },
  onLoad(query) {
    // 页面加载时
    this.setData({
      reasonActive: [],
      roomNumber: '',
      remark: '',
      image_path: [],
      imgList: [],
      imgListArr: []
    })
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: my.getStorageSync({
        key: 'hotel_name', // 缓存数据的key
      }).data,
      desc: my.getStorageSync({
        key: 'my_app_description', // 缓存数据的key
      }).data,
      path: 'pages/index/index?share=true',
      success: function (e) {
        console.log(e);
      }
    };
  },
});
